<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改任务调度')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-dispatcher-edit" th:object="${ktDispatcher}">
        <input name="id" th:field="*{id}" type="hidden">
        <input name="dpRepoType" id="dpRepoType" value="2" type="hidden" required>
        <input name="dpId" id="dpId" th:value="*{dpId}" type="hidden" required>
        <div class="form-group">
            <label class="col-sm-3 control-label">调度名称：</label>
            <div class="col-sm-8">
                <input name="dpName" th:field="*{dpName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group is-required">
            <label class="col-sm-3 control-label is-required">服务节点：</label>
            <div class="col-sm-8">
                <select id="carteId" name="carteId"  class="form-control" required>
                    <option th:each="node:${nodeList}" th:value="${node.nodeId}" th:text="${node.name}" th:selected="${node.nodeId == ktDispatcher.carteId}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">调度地址：</label>
            <div class="col-sm-8">
                <input id="dpPath" name="dpPath" th:field="*{dpPath}" class="form-control" type="text" readonly>
            </div>
        </div>
        <div class="form-group is-required">
            <label class="col-sm-3 control-label">参数组：</label>
            <div class="col-sm-8">
                <select id="paramsGroupId" name="paramsGroupId"  class="form-control">
                    <option></option>
                    <option th:each="pg:${pgList}" th:value="${pg.paramsGroupId}" th:text="${pg.paramsGroupName}" th:selected="${ktDispatcher.paramsGroupId==pg.paramsGroupId}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">预警信息：</label>
            <div class="col-sm-8">
                <select class="selectpicker" id="warnMsgIds" multiple placeholder="请选择通知">
                    <option>-</option>
                    <option th:each="warn:${warnList}" th:value="${warn.id}" th:selected="${@KtDispatcherServiceImpl.warnIdSelected('__${warn.id}__', '__*{warnMsgIds}__')}">
                        [[${warn.contactType == '1' ? '短信：'+warn.phoneNum: warn.contactType == '2' ? 'e-mail：'+warn.eMailAddr : '钉钉：'+warn.dingWebHook} ]]
                    </option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "busi/dispatcher";
    var nodeList = [[${nodeList}]];

    $(function (){
        $("#carteId").bind("change", function () {
            var carteId = $("#carteId option:selected").val();
            if (carteId === undefined || carteId === null || carteId === '') {
                $("#dpPath").val(null);
                return;
            } else {
                $("#dpPath").val();
            }

            for (var i = 0; i < nodeList.length; i++) {
                if (nodeList[i]["nodeId"] === carteId*1) {
                    $("#dpPath").val(nodeList[i]["nodeHost"]);
                }
            }

        });

    });

    $("#form-dispatcher-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            let serialize = $('#form-dispatcher-edit').serialize();
            let vals = $('#warnMsgIds').val();
            let warnMsgIds = "";
            for (var i = 0; i<vals.length; i++) {
                if (i === 0 ) {
                    if (vals[i] === '-') {
                        continue;
                    }
                    warnMsgIds = vals[i];
                    continue;
                }
                warnMsgIds += "," + vals[i];
            }
            if (warnMsgIds !== "") {
                serialize += "&warnMsgIds=" + warnMsgIds;
            }
            $.operate.save(prefix + "/edit", serialize);
        }
    }

    $("input[name='createdTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='updatedTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });
</script>
</body>
</html>